﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Data.OracleClient;

using DreamBuilder.Common;

namespace Web.SystemConfig.ArchiveCode
{
    /// <summary>
    /// 备注：档号流水号数据访问类
    /// </summary>
    public class ArchiveCodeNumberDAL : DataAccessBase
    {
        /// <summary>
        /// 获取指定档号类型的下一个流水号
        /// </summary>
        /// <param name="archiveType">档案类型</param>
        /// <param name="qzh">全宗号</param>
        /// <param name="wellcode">井号</param>
        /// <returns></returns>
        internal int GetNextNumber(WorkFlow.ArchiveTableEnum archiveType, string qzh, string wellcode = null)
        {
            string cmdText = "select currentnumber from archivecode_number where archivetype=:archivetype  and qzh=:qzh";
            List<OracleParameter> paramList = new List<OracleParameter>{
                new OracleParameter(":archiveType",(int)archiveType)
                ,new OracleParameter(":qzh",qzh)
            };
            if (archiveType == WorkFlow.ArchiveTableEnum.OliSearchK02)
            {
                paramList.Add(new OracleParameter(":wellcode", wellcode));
                cmdText = "select currentnumber from archivecode_number where archivetype=:archivetype  and qzh=:qzh  and wellcode=:wellcode";
            }
            
            object currentnumberObj = DBHelper.ExecuteScalar(cmdText, paramList.ToArray());
            int currentNumber = 0;
            if (currentnumberObj == null || string.IsNullOrEmpty(currentnumberObj.ToString()))
                currentNumber = 1;
            else
                int.TryParse(currentnumberObj.ToString(), out currentNumber);
            return currentNumber;
        }

        /// <summary>
        /// 备注：更新指定档案类型对应的流水号-有则更新无则添加
        /// 时间：2012年12月3日 23:03:56
        /// </summary>
        /// <param name="archiveType"></param>
        /// <param name="qzh">全宗号</param>
        /// <param name="currentNumber"></param>
        /// <param name="wellcode">井号</param>
        internal void UpdateCurrentNumber(WorkFlow.ArchiveTableEnum archiveType,string qzh, int currentNumber, string wellcode = null)
        {
            string cmdText = "select count(*) from archivecode_number where archivetype=:archivetype  and qzh=:qzh";
            List<OracleParameter> paramList = new List<OracleParameter>{
                new OracleParameter(":archiveType",(int)archiveType)
                ,new OracleParameter(":qzh",qzh)
            };
            if (archiveType == WorkFlow.ArchiveTableEnum.OliSearchK02)
            {
                paramList.Add(new OracleParameter(":wellcode", wellcode));
                cmdText = "select count(*) from archivecode_number where archivetype=:archivetype  and qzh=:qzh and wellcode=:wellcode";
            }
            object currentnumberObj = DBHelper.ExecuteScalar(cmdText, paramList.ToArray());
            paramList.Clear();
            if (currentnumberObj.ToString() == "0")
            {
                paramList = new List<OracleParameter>
                {
                    new OracleParameter(":archivetype",(int)archiveType),
                    new OracleParameter(":currentnumber",currentNumber),
                    new OracleParameter(":qzh",qzh)
                };
                cmdText = "insert into archivecode_number(archivetype,qzh,currentnumber)values(:archivetype,:qzh,:currentnumber)";
                if (archiveType == WorkFlow.ArchiveTableEnum.OliSearchK02)
                {
                    paramList.Add(new OracleParameter(":wellcode", wellcode));
                    cmdText = "insert into archivecode_number(archivetype,qzh,currentnumber, wellcode)values(:archivetype,:qzh,:currentnumber, :wellcode)";
                }
                DBHelper.ExecuteSql(cmdText,paramList.ToArray());
            }
            else
            {
                paramList = new List<OracleParameter>
                {
                    new OracleParameter(":archivetype",(int)archiveType),
                    new OracleParameter(":currentnumber",currentNumber),
                    new OracleParameter(":qzh",qzh)
                };
                cmdText = "update archivecode_number set currentnumber=:currentnumber where archivetype=:archivetype and qzh=:qzh";
                if (archiveType == WorkFlow.ArchiveTableEnum.OliSearchK02)
                {
                    paramList.Add(new OracleParameter(":wellcode", wellcode));
                    cmdText = "update archivecode_number set currentnumber=:currentnumber where archivetype=:archivetype and qzh=:qzh and wellcode=:wellcode";
                }
                DBHelper.ExecuteSql(cmdText,paramList.ToArray());
            }
        }
    }
}
